/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-10-14 10:49:10
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-12-23 10:23:46
 */
import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { LazyLoadEvent, SelectItem, TreeNode } from 'primeng/api';
import { Validators, FormControl, FormGroup, FormBuilder } from '@angular/forms';
import { DataExportComponent } from '@app/admin/bop/data-export/data-export.component'
import { QualityPlatformServiceProxy, CommonServiceProxy ,KeyWorkStepInput} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { LookPathComponent } from '../components/look-path/look-path.component';


@Component({
    selector: 'app-import-work',
    templateUrl: './import-work.component.html',
    styleUrls: ['./import-work.component.less']
})
export class ImportWorkComponent extends AppComponentBase {
    @ViewChild('path') lookPath: LookPathComponent;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    modelOptions = [];
    model = undefined;
    sendTimeOptions = [];
    sendTime = undefined;
    selectedFiles = undefined;
    loadingtree = false;
    treeData: any = undefined;
    propty = undefined;
    isCheckTreeNode: any = undefined;
    rowData: any = undefined;
    proptyOptions = [
        {
            label: '军检',
            value: '军检'
        }, {
            label: '三检',
            value: '三检'
        }, {
            label: '强制检验',
            value: '强制检验'
        },
        {
            label: '易错',
            value: '易错'
        },
        {
            label: '涉及分离',
            value: '涉及分离'
        },
        {
            label: '极性',
            value: '极性'
        },
        {
            label: '单点',
            value: '单点'
        },
        {
            label: '难操作',
            value: '难操作'
        },
        {
            label: '多余物防控',
            value: '多余物防控'
        },
        {
            label: '操作要点',
            value: '操作要点'
        }, {
            label: '成败',
            value: '成败'
        }
    ];
    //日期显示中文
    chinese = {
        /** 每周第一天，0代表周日 */
        firstDayOfWeek: 0,
        /** 每周天数正常样式 */
        dayNames: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
        /** 每周天数短样式（位置较小时显示） */
        dayNamesShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
        /** 每周天数最小样式 */
        dayNamesMin: ["日", "一", "二", "三", "四", "五", "六"],
        /** 每月月份正常样式 */
        monthNames: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        /** 每月月份短样式 */
        monthNamesShort: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]
    }
    rangeDates = undefined;
    constructor(injector: Injector,
        private _qualityPlatformServiceProxy: QualityPlatformServiceProxy,
        private _fileDownloadService: FileDownloadService,
        private _commonServiceProxy: CommonServiceProxy,
        private _messageService: MessageService
    ) {
        super(injector);
    }
    exprot() {
        if (this.isCheckTreeNode == undefined) {
            this._messageService.info('请先选择树节点');
            return;
        }
        let input = new KeyWorkStepInput();
        input.nodeId = this.isCheckTreeNode.id;
        input.processesCharacteristic = this.propty;
        if (this.rangeDates != undefined) {
            input.startData = this.rangeDates[0];
            input.endData = this.rangeDates[1];
        }
        this._qualityPlatformServiceProxy.exportKeyWorkExcel(
            input
        ).pipe(finalize(() => { })).subscribe(result => {
            window.open(result);
            URL.revokeObjectURL(result);
        });
    }
    showUrlInfo(row) {
        this.rowData = row;
        this.rowData.rWorkStepEntityId = row.id;
        setTimeout(() => {
            this.lookPath.show()
        }, 20)
    }
    ngOnInit() {
        //获取型号下拉
        this.modelOptions = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._qualityPlatformServiceProxy.getModelNameList(undefined)
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item;
                    data.value = item;
                    this.modelOptions.push(data);
                }
            });
    }
    onChange(event) {
        //获取发次
        this.sendTimeOptions = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._qualityPlatformServiceProxy.getLotNoList(undefined, this.model)
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item;
                    data.value = item;
                    this.sendTimeOptions.push(data);
                }
            });
    }
    treeSearch() {
        //树
        if (!Boolean(this.model && this.sendTime)) {
            this._messageService.warn('请先选择型号和发次!')
            return
        }
        this._qualityPlatformServiceProxy.getTreeContainRoot(this.sendTime, this.model)
            .pipe(finalize(() => { })).subscribe(result => {
                this.treeData = [];
                this.treeData.push({
                    ...result.parentNode,
                    key: -1,
                    nodeDrawingNo: result.parentNode.drawingNo,
                    nodeName: result.parentNode.modelName,
                    model: result.parentNode.model,
                    lotNo: result.parentNode.lotNo,
                    stageSign: result.parentNode.stageSign,
                    children: result.childNodes
                })
                this.expandAll();
            })
    }
    //全部展开
    expandAll() {
        this.treeData.forEach((node) => {
            this.expandRecursive(node, true);
        });
    }
    private expandRecursive(node: TreeNode, isExpand: boolean) {
        node.expanded = isExpand;
        if (node.children) {
            node.children.forEach((childNode) => {
                this.expandRecursive(childNode, isExpand);
            });
        }
    }
    nodeSelect(event) {
        //点击事件
        this.isCheckTreeNode = event.node;
    }
    searchRight(event?: LazyLoadEvent) {
        if (this.isCheckTreeNode == undefined) {
            this._messageService.info('请先选择树节点');
            return;
        }
        let startTime = undefined;
        let endTime = undefined;
        if (this.rangeDates != undefined) {
            // startTime = moment(this.rangeDates[0]).format('YYYY-MM-DD ');
            // endTime = moment(this.rangeDates[1]).format('YYYY-MM-DD ');
            startTime = this.rangeDates[0];
            endTime = this.rangeDates[1];
        }
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this._qualityPlatformServiceProxy.getKeyWork(
            this.isCheckTreeNode.id,
            this.propty,
            startTime,
            endTime,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }
    resert() {
        this.propty = undefined;
        this.rangeDates = undefined;
    }
}
